package com.example.com.skills_utf8;



import view.AFragment;
import view.BFragment;

import com.example.com.skills_utf8.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.os.Bundle;
import android.widget.Toast;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
/**
 * This demonstrates the use of action bar tabs and how they interact
 * with other action bar features.
 */
public class FragmentTabs extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final ActionBar bar = getActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

        bar.addTab(bar.newTab()
                .setText("电器")
                .setTabListener(new TabListener<AFragment>(
                        this, "A", view.AFragment.class)));
        bar.addTab(bar.newTab()
                .setText("图书")
                .setTabListener(new TabListener<BFragment>(
                        this, "B", view.BFragment.class)));
//        bar.addTab(bar.newTab()
//                .setText("Apps")
//                .setTabListener(new TabListener<LoaderCustom.AppListFragment>(
//                        this, "apps", LoaderCustom.AppListFragment.class)));
//        bar.addTab(bar.newTab()
//                .setText("Throttle")
//                .setTabListener(new TabListener<LoaderThrottle.ThrottledLoaderListFragment>(
//                        this, "throttle", LoaderThrottle.ThrottledLoaderListFragment.class)));

        if (savedInstanceState != null) {
            bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
    }

    public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
        private final FragmentActivity mFragmentActivity;
        private final String mTag;
        private final Class<T> mClass;
        private final Bundle mArgs;
        private Fragment mFragment;

        public TabListener(FragmentActivity FragmentActivity, String tag, Class<T> clz) {
            this(FragmentActivity, tag, clz, null);
        }

        public TabListener(FragmentActivity FragmentActivity, String tag, Class<T> clz, Bundle args) {	
            mFragmentActivity = FragmentActivity;
            mTag = tag;
            mClass = clz;
            mArgs = args;

            // Check to see if we already have a fragment for this tab, probably
            // from a previously saved state.  If so, deactivate it, because our
            // initial state is that a tab isn't shown.
            
            
            mFragment= mFragmentActivity.getSupportFragmentManager().findFragmentByTag(mTag);
           // mFragment = mFragmentActivity.getFragmentManager().findFragmentByTag(mTag);
            if (mFragment != null && !mFragment.isDetached()) {
                FragmentTransaction ft = mFragmentActivity.getSupportFragmentManager().beginTransaction();
                ft.detach(mFragment);
                ft.commit();
            }
        }

        public void onTabSelected(Tab tab, android.app.FragmentTransaction f) {
        	FragmentTransaction ft = mFragmentActivity.getSupportFragmentManager().beginTransaction();
            if (mFragment == null) {
                mFragment = Fragment.instantiate(mFragmentActivity, mClass.getName(), mArgs);
                ft.add(android.R.id.content, mFragment, mTag);
            } else {
                ft.attach(mFragment);
            }
            ft.commit();
        }

        public void onTabUnselected(Tab tab, android.app.FragmentTransaction f) {
        	FragmentTransaction ft = mFragmentActivity.getSupportFragmentManager().beginTransaction();
            if (mFragment != null) {
                ft.detach(mFragment);
            }
            ft.commit();
        }

        public void onTabReselected(Tab tab, android.app.FragmentTransaction f) {
            Toast.makeText(mFragmentActivity, "Reselected!", Toast.LENGTH_SHORT).show();
        }

	

	
        
    }
}

